package facilius.controller.action.turma;

import facilius.model.ServiceLocator;
import facilius.model.pojo.Disciplina;
import facilius.model.pojo.Turma;
import facilius.model.pojo.Usuario;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class TurmaCreateAction extends BaseAction {

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        try {
            Turma turma = new Turma();
            turma.setDescricao(input.getString("descricao"));
            turma.setAno(input.getInt("ano"));

            Long disciplinaId = input.getLong("disciplinaId");
            Disciplina d = ServiceLocator.getDisciplinaService().readById(disciplinaId);
            
            turma.setDisciplina(d);

            Long idProf = input.getLong("professorId");
            Usuario professor = ServiceLocator.getUsuarioService().readById(idProf);

            turma.setProfessor(professor);

            ServiceLocator.getTurmaService().create(turma);
            consequence = SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }
}
